<?php
namespace app\system\model;

use think\Model;

class GroupModel extends Model
{
    protected $table='__GROUP__';

    /**
     * @param array $where
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function selectGroup(array $where=[])
    {
        return $this->where($where)->order('id desc')->select();
    }
    public function addGroup(array $data)
    {
        $addData = [
            'title' => $data['title'],
            'time' => time()
        ];
        return $this->insert($addData);
    }

    public function updateGroup(array $data, array $where)
    {
        return $this->where($where)->update($data);
    }

    public function deleteGroup($id)
    {
        if ($this->where(['id'=>$id])->delete()) {
            $groupLinkModel = new GroupLinkModel();
            $groupLinkModel->deleteGroupLink(['group_id'=>$id]);
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findGroup($id)
    {
        return $this->where(['id'=>$id])->find();
    }
}